﻿#建议保存编码为：bom头 + utf8

# 目标机器IP地址
$k8s_master1 = "192.168.1.2"
$k8s_node1 = "192.168.1.3"

#问：这个脚本谁写的？有问题找谁技术支持？
#答：QQ群号=700816263
#名称=kasini3000官方支持

# 定义安装Docker的脚本块
$install_docker = {
	# 卸载旧版本Docker
	sudo dnf remove -y docker docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-engine

	# 添加Docker仓库
	sudo dnf-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

	# 安装Docker
	sudo dnf install -y docker-ce-25.0.5-1.el9

	# 启动并设置开机自启
	systemctl daemon-reload
	sudo systemctl enable --now docker

	# 配置cgroupdriver
	@'
{
    "exec-opts": ["native.cgroupdriver=systemd"]
}
'@ | Add-Content /etc/docker/daemon.json

	# 重启Docker
	sudo systemctl restart docker

	#内核：
	#/etc/sysctl.d/sysctl.conf     #ubuntu /etc/sysctl.conf

	@'
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
vm.swappiness=0
'@ | Set-Content  /etc/sysctl.d/sysctl.conf
	sysctl -p /etc/sysctl.d/sysctl.conf
}

# 定义安装Kubernetes的脚本块
$install_k8s = {
	# 关闭SELinux和防火墙
	sudo setenforce 0
	sudo sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config
	sudo systemctl disable --now firewalld

	# 关闭swap
	sudo swapoff -a
	sudo sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab

	# 添加Kubernetes仓库
	@'
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes-new/core/stable/v1.31/rpm/
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
'@ | Set-Content /etc/yum.repos.d/kubernetes.repo

	# 安装指定版本的kubeadm/kubelet/kubectl
	sudo dnf install -y kubeadm-1.31.13 kubelet-1.31.13 kubectl-1.31.13

	# 设置kubelet开机自启
	systemctl daemon-reload
	sudo systemctl enable --now kubelet

	#ipvs
	dnf install ipvsadm ipset -y

	@'
#!/bin/bash
modprobe -- ip_vs
modprobe -- ip_vs_rr
modprobe -- ip_vs_wrr
modprobe -- ip_vs_sh
modprobe -- nf_conntrack

'@ | Set-Content /etc/sysconfig/modules/ipvs.modules

	chmod +x /etc/sysconfig/modules/ipvs.modules

	dnf install -y libcgroup kubernetes-cni
	rpm -ivh https://github.com/Mirantis/cri-dockerd/releases/download/v0.3.17/cri-dockerd-0.3.17-3.fc36.x86_64.rpm

	psed.ps1 -infile '/usr/lib/systemd/system/cri-docker.service' -find 'ExecStart' -replace 'ExecStart=/usr/bin/cri-dockerd --pod-infra-container-image=registry.aliyuncs.com/google_containers/pause:3.10'
	systemctl enable cri-docker
	systemctl daemon-reload
	systemctl start cri-docker

}


$init_cluster = {
	# 初始化控制平面 (使用flannel网络)
	sudo kubeadm init --pod-network-cidr=10.244.0.0/16 --kubernetes-version=1.31.13 --certificate-validity=86400 --upload-certs --cri-socket=unix:///var/run/cri-dockerd.sock

	# 配置普通用户使用kubectl
	mkdir -p $HOME/.kube
	sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
	sudo chown $(id -u):$(id -g) $HOME/.kube/config

	# 安装flannel网络插件
	kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
}



# 在master上执行
./k_run_ip -ip $k8s_master1 {
	# 安装Docker
	& $args[0]

	# 安装Kubernetes
	& $args[1]

	# 初始化集群
	& $args[2]
} -allparameter $install_docker, $install_k8s, $init_cluster

#在node上执行
./k_run_ip -ip $k8s_node1 {
	# 安装Docker
	& $args[0]

	# 安装Kubernetes
	& $args[1]
} -allparameter $install_docker, $install_k8s

$joincmd = ./k_run_ip -ip $k8s_master1 {
	kubeadm token create --print-join-command
}

./k_run_ip -ip $k8s_node1 {
	& $args[0] $args[1]
} -allparameter $joincmd ,'--cri-socket=unix:///var/run/cri-dockerd.sock'


# 检查集群状态 在master上执行
Write-Host "验证集群状态:"
./k_run_ip -ip $k8s_master1 {
	kubectl get nodes
	kubectl get pods --all-namespaces
}


# 注意：此脚本是给菜鸟准备的例子。对于master1，node1等，建议拆分脚本，开多个窗口，分别运行。
#kasini3000专业人士，应该使用 c:\ProgramData\kasini3000\k_commit_ip_rs.ps1 运行上述类似代码。
#欢迎帮忙修正更新脚本。


